
#include "system_gd32f1x0.h"
#include "gd32f1x0_conf.h"
#include "osObjects.h"  
#include "systimer.h"
#include "mbdatacb.h"
#include "touch.h"
#include "peripheral.h"
#include "at24xx.h" 
#include "pid.h"
#include "iap.h"
#include "tempctr.h"

/*----------------------------------------------------------------------------
 *      Global Functions
 *---------------------------------------------------------------------------*/

/*--------------------------- os_idle_demon ---------------------------------*/
   
uint32_t uuid[3];
uint32_t memInfo;

uint32_t IdleTaskCnt;

uint8_t  DataBuff[256];

uint8_t  KeyVal;
uint8_t  KeyCur;
uint8_t  KeyLast;
uint8_t  KeyDownEdge;
uint8_t  KeyUpEdge;
uint8_t  KeyChange;
uint8_t  bFirstKey;

const uint8_t SysData[16][128] @0x08008000 = 
{
    "What a lovely day ! I will do something !",
	"Hellow, where are you ?",
    "What a lovely day !",
	"Hellow, where are you ?",
	"What a lovely day !",
	"Hellow, where are you ?",
    "What a lovely day !",
	"Hellow, where are you ?",
};

#define  KEY_B2_MASK                   0x01
#define  KEY_B3_MASK                   0x80
#define  KEY_B4_MASK                   0x40

#define  bKeyB2                        ((uint8_t)(KeyVal & KEY_B2_MASK))
#define  bKeyB3                        ((uint8_t)(KeyVal & KEY_B3_MASK))
#define  bKeyB4                        ((uint8_t)(KeyVal & KEY_B4_MASK))

#define  bKeyB2DownEdge                ((uint8_t)(KeyDownEdge & KEY_B2_MASK))
#define  bKeyB3DownEdge                ((uint8_t)(KeyDownEdge & KEY_B3_MASK))
#define  bKeyB4DownEdge                ((uint8_t)(KeyDownEdge & KEY_B4_MASK))

#define  bKeyB2UpEdge                  ((uint8_t)(KeyUpEdge & KEY_B2_MASK))
#define  bKeyB3UpEdge                  ((uint8_t)(KeyUpEdge & KEY_B3_MASK))
#define  bKeyB4UpEdge                  ((uint8_t)(KeyUpEdge & KEY_B4_MASK))

#define  bKeyB2Change                  ((uint8_t)(KeyChange & KEY_B2_MASK))
#define  bKeyB3Change                  ((uint8_t)(KeyChange & KEY_B3_MASK))
#define  bKeyB4Change                  ((uint8_t)(KeyChange & KEY_B4_MASK))

/// \brief The idle demon is running when no other thread is ready to run
void os_idle_demon (void)
{
	uint32_t Cnt;
	
	uuid[0] = *((const uint32_t *) 0x1FFFF7AC);
	uuid[1] = *((const uint32_t *) 0x1FFFF7B0);
	uuid[2] = *((const uint32_t *) 0x1FFFF7B4);
	memInfo = *((const uint32_t *) 0x1FFFF7E0);
	
	SysTickInit();
	TouchInit();
	TempCtrInit();
	
	Cnt = 10;
	while(Cnt--)
	{
		DataBuff[Cnt] = SysData[0][Cnt];
	}
	at24Write(0,10,DataBuff);
	at24Read(0,256,DataBuff);
	TempCtrInit();
	
	Cnt = 128;
	while(Cnt--)
		DataBuff[Cnt] = Cnt << 1;
	
	//FlashWriteInPage( 0x08008400, DataBuff, 128);
	
    for (;;)
	{
		SysPulseClear();
		SysPulseCreat();
	
		/* HERE: include optional user code to be executed when no thread runs.*/
		IdleTaskCnt++;
		
		TempCtrTask();
		
		TouchCheck();
		
		if(bPulse_125ms)
		{
			GPIOB->TGR = GPIO_TGR_TG10;		
		}		
		
		if(bPulse_3s)
		{
			GPIOB->TGR = GPIO_TGR_TG8;		
		}
		
		/*
		if(bPulse_3s)
		{
			GPIOB->TGR = GPIO_TGR_TG9;	
		}
		*/
		
		if(bPulse_20ms)
		{
			KeyCur = ~((uint8_t)(GPIOB->DIR)) & 0xC0;
			KeyCur |= ~((uint8_t)(GPIOA->DIR)) & 0x01;
		}
		
		{
			if(KeyCur == KeyLast)
			{
				if(KeyVal != KeyCur)
				{
					bFirstKey = 1;
					KeyChange = KeyVal ^ KeyCur;
					KeyDownEdge = KeyChange & KeyCur;
					KeyUpEdge = KeyChange & (~KeyCur);
					KeyVal = KeyCur;
				}
				else
				{
					bFirstKey = 0;
					KeyDownEdge = 0;
					KeyUpEdge = 0;
					KeyChange = 0;
				}
			}
			
			KeyLast = KeyCur;		
		}
		
		if(bKeyB2DownEdge)
		{
			RegHolding[0] ++;
		}
		
		if(bKeyB3DownEdge)
		{
			RegHolding[1] ++;
		}

		if(bKeyB4DownEdge)
		{
			RegHolding[2] ++;
		}		

		if(bKeyB2UpEdge)
		{
			RegHolding[6] ++;
		}
		
		if(bKeyB3UpEdge)
		{
			RegHolding[7] ++;
		}

		if(bKeyB4UpEdge)
		{
			RegHolding[8] ++;
		}
		
		if(bPulse_3s)
		{			
			
		}
		
		Cnt = 15;
		while(Cnt--)
		{
			RegHolding[Cnt] = AdcResault[Cnt];
		}
		
		if(SysTimeCnt > 3)
		{
			if(bPulse_1s)
			{
				if(DMA_GetBitState(DMA1_FLAG_TC1))
				{
					DMA_ClearBitState(DMA1_FLAG_TC1);
				}
				
				if(DMA_GetBitState(DMA1_INT_ERR1))
				{
					DMA_ClearBitState(DMA1_FLAG_TC1);
				}
				
				/*
				if(ADC_GetBitState(ADC_FLAG_EOC) || (!ADC_GetBitState(ADC_FLAG_STIC)))
				{
					ADC1->CTLR2 |= ADC_CTLR2_SWRCST;                                    //start inserted group
					ADC1->STR    = 0x1F;
				}
				*/
			}
		}
		else
		{
			if(3 == SysTimeCnt)
			{
				if(bPulse_1s)
				{
					ADC_Calibration();
					if(!ADC_GetBitState(ADC_FLAG_STRC))
					{
						ADC1->CTLR2 |= ADC_CTLR2_SWRCST;                               //start regular group
					}					
				}
			}
		}
		//Task end
    }
}

